<?php
include_once 'controller/default.php';
include_once 'connection/connection_database.php';
include_once 'checkForm/xulychuoi.php';

class boards {

    function Insert($board) {
        $str = trim(khongdau($_POST['boardname']));
        $link = strtolower(preg_replace('/[^a-z0-9]+/i','-',$str)); 
        $fillAll = "SELECT * FROM cate_board WHERE board_url LIKE '%{$link}%'";
        $rs = mysql_query($fillAll);
        if(($rows = mysql_num_rows($rs)) > 0){
            $link .= "-".$rows;
        }
        $sql = "INSERT INTO cate_board(board_name, category_id, board_url) VALUES('{$board["boardname"]}', '{$board["cate_name"]}', '{$link}')";
        $result = mysql_query($sql) or die(mysql_error());
        return $result;
    }

    function Delete($board) {
        $sql = "DELETE FROM cate_board WHERE board_id='{$board["id"]}'";
        $result = mysql_query($sql) or die(mysql_error());
        return $result;
    }

    function Update($board) {
        $str = trim(khongdau($_POST['boardname']));
        $link = strtolower(preg_replace('/[^a-z0-9]+/i','-',$str)); 
        $fillAll = "SELECT * FROM cate_board WHERE board_url LIKE '%{$link}%' and board_id<>{$board["id"]}";
        $rs = mysql_query($fillAll);
        if(($rows = mysql_num_rows($rs)) > 0){
            $link .= "-".$rows;
        }
        $sql = "UPDATE cate_board SET board_name='{$board["boardname"]}', category_id='{$board["cate_name"]}', board_url='{$link}' WHERE board_id='{$board["id"]}'";
        $result = mysql_query($sql) or die(mysql_error());
        return $result;
    }

    function UpdateOrder($board) {
        $boolean = false;
        foreach ($board["board_name"] as $_board) {
            //$bId = json_decode(json_decode('"' . $_board . '"'), true);
			$bId = json_decode($_board, true);
            $sql = "UPDATE cate_board SET board_order='{$bId["order"]}' WHERE board_id='{$bId["board_id"]}'";
            $result = mysql_query($sql) or die(mysql_error());
            if($result){
                $boolean = true;
            } else {
                return false;
            }
        }
        return $boolean;
    }
    
    function FillAll() {
        $sql = "SELECT * FROM cate_board cb, category c WHERE c.category_id=cb.category_id and cb.board_status=1";
        $result = mysql_query($sql) or die(mysql_error());
        return $result;
    }

    function FillOne($board) {
        $sql = "SELECT * FROM cate_board WHERE board_id='{$board["id"]}'";
        $result = mysql_query($sql) or die(mysql_error());
        return mysql_fetch_array($result);
    }
}

?>
